Author: SAI K
In Java, strings are a fundamental data type used extensively in programming. Often, there's a need to iterate over each character in a string, whether for character counting, data validation, transformation, or other purposes. Java provides several ways to iterate over the characters of a string, each with its own use cases and benefits. In this blog post, we'll explore various methods to iterate the String characters in Java.
charAt()
in a LoopThe most straightforward method to iterate over characters in a string is using a for
loop along with the charAt()
method.
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
System.out.println("Character at position " + i + " is: " + ch);
}
}
}
Character at position 0 is: H
Character at position 1 is: e
Character at position 2 is: l
Character at position 3 is: l
Character at position 4 is: o
Character at position 5 is: ,
Character at position 6 is:
Character at position 7 is: W
Character at position 8 is: o
Character at position 9 is: r
Character at position 10 is: l
Character at position 11 is: d
Character at position 12 is: !
In this example:
for
loop to go through each index of the string.charAt(i)
returns the character at the specified index.toCharArray()
and a For-Each LoopAnother method is to convert the string to a character array using toCharArray()
and then use a for-each loop.
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
for (char ch : str.toCharArray()) {
System.out.println("Character: " + ch);
}
}
}
Character: H
Character: e
Character: l
Character: l
Character: o
Character: ,
Character:
Character: W
Character: o
Character: r
Character: l
Character: d
Character: !
toCharArray()
converts the string into a character array.For a more functional approach, especially in Java 8 and later, you can use streams to iterate over characters.
import java.util.stream.IntStream;
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, World!";
IntStream charsStream = str.chars();
charsStream.forEach(ch -> System.out.println("Character: " + (char) ch));
}
}
Character: H
Character: e
Character: l
Character: l
Character: o
Character: ,
Character:
Character: W
Character: o
Character: r
Character: l
Character: d
Character: !
str.chars()
creates an IntStream
representing the character values of the string.forEach
is used to iterate over each character in the stream.codePoints()
for Unicode StringsIf you are dealing with Unicode strings that may have surrogate pairs, use codePoints()
.
public class StringIteration {
public static void main(String[] args) {
String str = "Hello, 👋 World!";
str.codePoints().forEach(cp -> System.out.println("Codepoint: " + cp + ", Character: " + new String(Character.toChars(cp))));
}
}
Codepoint: 72, Character: H
Codepoint: 101, Character: e
Codepoint: 108, Character: l
Codepoint: 108, Character: l
Codepoint: 111, Character: o
Codepoint: 44, Character: ,
Codepoint: 32, Character:
Codepoint: 128075, Character: 👋
Codepoint: 32, Character:
Codepoint: 87, Character: W
Codepoint: 111, Character: o
Codepoint: 114, Character: r
Codepoint: 108, Character: l
Codepoint: 100, Character: d
Codepoint: 33, Character: !
codePoints()
returns a stream of Unicode code points.forEach
method is used to iterate over each code point.Iterating the string characters in Java can be done in multiple ways, each suitable for different scenarios. Traditional loops work well